/**
 * Created by DELL on 2017/5/12.
 */
var rewardId=common.getQueryString("rewardId");
var userInfo=common.getLocalData(common.agentSysCacheWord).user;

var $form=$("form");
var validateForm=$form.validate({
    /**
     * ignore：忽略某些元素不验证
     * @type {String}
     * 例如：ignore: ".ignore" ，忽略所有的带ignore class 的标签
     */
    ignore: ".ignore"
});
window.formObject = $form.submit(function() {
    if(validateForm.form()){
        var data = common.formToJson($(this));
        if(!data.coverPhoto){
            layer.alert("请上传封面图片");
            return false;
        }
        if(!data.housePhotos){
            layer.alert("请上传悬赏房源图片");
            return false;
        }
        $.post(window.globalHost + common.rewardInfoUpdatePath, data).success(function(data) {
            layer.alert(data.res.msg);
            if ("0" == data.code) {
                //如果更新成功，重新渲染页面
                window.location.reload();
            }
        });
    }
    return false;
});
renderPage();
function renderPage() {
    $.post(window.globalHost+common.rewardInfoPath,{id:rewardId}).success(function (data) {
        var detailInfo=common.transformData(data);
        common.initInputByName(detailInfo);
        buildSelect.buildEmployeeList("#contact",detailInfo.userId);
        common.initDivById({contactTel:detailInfo.contactTel});
        var coverPhotoGallery=$("#cover-photo").gallery({
            // readOnly:record.saleStatus==="SOLD",
            readOnly:false,
            title:"上传悬赏房源封面",
            url:window.globalHost +common.uploadImagePort,
            paramName:"uploadData",
            fileFolder:{folder:"house",id:userInfo.id,fileName:"sys_esheyiHouse_"+new Date().getTime()},
            returnPathName:"filePath",
            maxFiles:1,
            filePaths:detailInfo.coverPhoto?[detailInfo.coverPhoto]:[],
            uploadCallback:function () {
                var path=coverPhotoGallery.getFilePaths();
                $("#coverPhoto").val(!!path.length?path.join(","):"")
            },
            deleteCallback:function () {
                var path=coverPhotoGallery.getFilePaths();
                $("#coverPhoto").val(!!path.length?path.join(","):"");
            }
        });
        var housePhotosGallery=$("#reward-photos").gallery({
            // readOnly:record.saleStatus==="SOLD",
            readOnly:false,
            title:"上传悬赏房源图片",
            url:window.globalHost + common.uploadImagePort,
            paramName:"uploadData",
            fileFolder:{folder:"house",id:userInfo.id,fileName:"sys_esheyiHouse_"+new Date().getTime()},
            returnPathName:"filePath",
            maxFiles:20,
            filePaths:detailInfo.housePhotos?detailInfo.housePhotos.split(","):[],
            uploadCallback:function () {
                var path=housePhotosGallery.getFilePaths();
                $("#housePhotos").val(!!path.length?path.join(","):"")
            },
            deleteCallback:function () {
                var path=housePhotosGallery.getFilePaths();
                $("#housePhotos").val(!!path.length?path.join(","):"");
            }
        });
    });
}